import 'package:flutter/material.dart';
import 'package:flutter_demo/chapter8-EventNotification/event-mechanism/HitTestBlocker.dart';
import 'package:flutter_demo/chapter8-EventNotification/gesture-conflict/CustomTapGestureRecongnizer.dart';

void main() {
  runApp(MaterialApp(
    home: Scaffold(
      appBar: AppBar(
        title: const Text("Animation Widget Demo"),
      ),
      body: ImageAnimatedWidget(),
    ),
  ));
}

class ImageAnimatedWidget extends StatefulWidget {
  const ImageAnimatedWidget({Key? key}) : super(key: key);

  @override
  _ImageAnimatedWidgetState createState() => _ImageAnimatedWidgetState();
}

class _ImageAnimatedWidgetState extends State<ImageAnimatedWidget>
    with SingleTickerProviderStateMixin {
  late Animation<double> animation;
  late AnimationController controller;

  @override
  Widget build(BuildContext context) {
    return GrowTransition(
      animation: animation,
      child: Image.asset("assets/images/avatar.png"),
    );
  }

  @override
  void initState() {
    super.initState();
    controller = AnimationController(
      duration: const Duration(seconds: 3),
      vsync: this,
    );

    //  匀速
    // 图片宽高从0变到300
    animation = Tween(begin: 0.0, end: 300.0).animate(controller);
    animation.addStatusListener((status) {
      if (status == AnimationStatus.completed) {
        //动画执行结束时反向执行动画
        controller.reverse();
      } else if (status == AnimationStatus.dismissed){
        //  动画恢复到初始时执行动画(正向执行)
        controller.forward();
      }
    });
    //  启动动画(正向执行)
    controller.forward();
  }

  @override
  void dispose() {
    //  路由销毁释放动画资源
    controller.dispose();
    super.dispose();
  }
}

class GrowTransition extends StatelessWidget {
  const GrowTransition({Key? key, required this.animation, this.child})
  : super(key: key);
  final Widget? child;
  final Animation<double> animation;

  @override
  Widget build(BuildContext context) {
    return Center(
      child: AnimatedBuilder(
        animation: animation,
        builder: (context, child) {
          return SizedBox(
            height: animation.value,
            width: animation.value,
            child: child,
          );
        },
        child: child,
      ),
    );
  }
}
